"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.DialogWebLayout = void 0;

var _react = _interopRequireWildcard(require("react"));

var _configContext = require("../../../extends/configContext");

var _classnames = _interopRequireDefault(require("classnames"));

var _webPortal = _interopRequireDefault(require("../../../common/web-portal"));

var _icon = _interopRequireDefault(require("../../icon"));

var _button = _interopRequireDefault(require("../../button"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var DialogWebLayout = /*#__PURE__*/function (_Component) {
  _inherits(DialogWebLayout, _Component);

  var _super = _createSuper(DialogWebLayout);

  function DialogWebLayout() {
    _classCallCheck(this, DialogWebLayout);

    return _super.apply(this, arguments);
  }

  _createClass(DialogWebLayout, [{
    key: "getDialogWidth",
    value: function getDialogWidth() {
      var width = this.props.width;
      var containerWidth = typeof width === 'number' ? "".concat(width, "px") : width;
      if (typeof window !== 'undefined' && width && parseInt(width.toString(), 10) >= window.innerWidth) containerWidth = "".concat(window.innerWidth - 20, "px");
      return {
        width: containerWidth
      };
    }
  }, {
    key: "render",
    value: function render() {
      var _this = this;

      var clsPrefix = this.context.clsPrefix;
      var _this$props = this.props,
          children = _this$props.children,
          style = _this$props.style,
          zIndex = _this$props.zIndex,
          attach = _this$props.attach,
          onClose = _this$props.onClose,
          title = _this$props.title,
          header = _this$props.header,
          footer = _this$props.footer,
          cancelBtn = _this$props.cancelBtn,
          confirmBtn = _this$props.confirmBtn,
          confirmDisabled = _this$props.confirmDisabled,
          confirmLoading = _this$props.confirmLoading,
          onCancel = _this$props.onCancel,
          type = _this$props.type,
          onConfirm = _this$props.onConfirm;

      var reStyle = _objectSpread(_objectSpread({}, style), {}, {
        zIndex: zIndex
      });

      var width = this.props.visible ? this.getDialogWidth() : {};

      var headerDom = /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _classnames.default)("".concat(clsPrefix, "-modal__header"), this.props.headerClassName),
        style: this.props.headerStyle
      }, header && header, !header && (this.props.isNew || this.props.inContext) && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, title, /*#__PURE__*/_react.default.createElement(_icon.default, {
        className: "".concat(clsPrefix, "-header__closeicon"),
        name: "CloseOutlined",
        size: 12,
        onClick: function onClick() {
          return onClose();
        }
      })));

      var footerDom = /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _classnames.default)("".concat(clsPrefix, "-modal__footer"), this.props.footerClassName),
        style: this.props.footerStyle
      }, footer && footer, !footer && (this.props.isNew || this.props.inContext) && /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, type === 'confirm' && /*#__PURE__*/_react.default.createElement(_button.default, {
        className: "".concat(clsPrefix, "-modal_cancel"),
        onClick: function onClick() {
          return onCancel();
        }
      }, cancelBtn), /*#__PURE__*/_react.default.createElement(_button.default, {
        type: "primary",
        disabled: confirmDisabled,
        loading: confirmLoading,
        onClick: function onClick() {
          return onConfirm();
        }
      }, confirmBtn)));

      if (!header && !this.props.isNew) headerDom = null;
      if (!footer && !this.props.isNew) footerDom = null;

      var dialogDom = /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _classnames.default)("".concat(clsPrefix, "-modal"), {
          'is-active': this.props.visible
        }, this.props.className),
        style: reStyle
      }, /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _classnames.default)("".concat(clsPrefix, "-modal__overlay"), {
          'animated animated-fadeIn': this.props.visible,
          'animated animated-fadeOut': !this.props.visible
        }),
        style: this.props.maskStyle,
        onClick: function onClick() {
          if (_this.props.maskClosable) {
            _this.props.onClose();
          }
        }
      }), /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _classnames.default)("".concat(clsPrefix, "-modal__container"), {
          'animated animated-zoomIn': this.props.visible,
          'animated animated-zoomOut': !this.props.visible
        }),
        style: width
      }, headerDom, /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _classnames.default)("".concat(clsPrefix, "-modal__body"), this.props.bodyClassName),
        style: this.props.bodyStyle
      }, children), footerDom));

      return /*#__PURE__*/_react.default.createElement(_webPortal.default, {
        attach: attach
      }, dialogDom);
    }
  }]);

  return DialogWebLayout;
}(_react.Component);

exports.DialogWebLayout = DialogWebLayout;

_defineProperty(DialogWebLayout, "contextType", _configContext.ConfigContext);